from django import template
from django.conf import settings
import os

register = template.Library()

@register.filter
def after_storage(value):
    """返回file_storage之后的路径部分"""
    parts = value.split('file_storage/')
    return parts[-1] if len(parts) > 1 else value

@register.filter
def relpath(value):
    """将完整路径转换为相对于FILE_ROOT的路径"""
    # 标准化路径
    normalized_path = os.path.normpath(value)
    normalized_root = os.path.normpath(settings.FILE_ROOT)
    
    # 获取相对路径
    relative_path = os.path.relpath(normalized_path, normalized_root)
    
    # 处理Windows路径分隔符
    relative_path = relative_path.replace('\\', '/')
    
    return relative_path
